﻿# import unittest
import sys

from django.contrib.staticfiles.testing import StaticLiveServerTestCase

from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class NewVisitorTest(StaticLiveServerTestCase):

	@classmethod
	def setUpClass(cls):
		for arg in sys.argv:
			if 'liveserver' in arg:
				cls.server_url = 'http://' + arg.split('=')[1]
				return
		super().setUpClass()
		cls.server_url = cls.live_server_url

	@classmethod
	def tearDownClass(cls):
		if cls.server_url == cls.live_server_url:
			super().tearDownClass()

	def setUp(self):
		self.browser = webdriver.Firefox()
		self.browser.implicitly_wait(3)

	def tearDown(self):
		self.browser.refresh()
		self.browser.quit()


	def check_for_row_in_list_table(self, row_text):
		table = self.browser.find_element_by_id("id_list_table")
		rows = table.find_elements_by_tag_name("tr")
		self.assertIn(row_text,[row.text for row in rows])


	# **************用户故事***************

	def test_can_start_a_list_and_retieve_it_later(self):
		# Jane听说有个很酷的在线待办事项应用
		# 她去看了这个应用的首页
		self.browser.set_window_size(1024, 768)
		self.browser.get(self.server_url)

		# 她注意到这个网页的标题和头部都包含“To-Do”这个词
		self.assertIn("To-Do", self.browser.title)
		header_text = self.browser.find_element_by_tag_name("h1").text
		self.assertIn('To-Do', header_text)
		
		# 她看到输入框居中显示
		# inputbox = self.browser.find_element_by_id("id_new_item")
		# self.assertAlmostEqual(inputbox.location['x'] + inputbox.size['width'] / 2, 512, delta=5)

		# 应用邀请她输入一个待办事项
		inputbox = self.browser.find_element_by_id("id_new_item")
		self.assertEqual(inputbox.get_attribute("placeholder"),
			"Enter a to-do item")

		# 她在文本框中输入了“Buy peacock feathers”
		# Jane的爱好是使用假蝇做饵钓鱼
		inputbox.send_keys("Buy peacock feathers")

		# 她按回车键后，被带到一个新的URL
		# 这个页面的待办事项清单中显示了“1: Buy peacock feathers”
		inputbox.send_keys(Keys.ENTER)
		self.browser.implicitly_wait(3)
		jane_list_url = self.browser.current_url
		self.assertRegex(jane_list_url, '/lists/.+')
		self.check_for_row_in_list_table("1: Buy peacock feathers")

		# 页面中又显示了一个文本框，可以输入其他的待办事项
		# 她输入了“Use peacock feathers to make a fly”
		inputbox = self.browser.find_element_by_id("id_new_item")
		inputbox.send_keys("Use peacock feathers to make a fly")

		# 页面再次更新， 她的清单中显示了这两个待办事项
		inputbox.send_keys(Keys.ENTER)
		self.browser.implicitly_wait(3)

		self.check_for_row_in_list_table("1: Buy peacock feathers")
		self.check_for_row_in_list_table("2: Use peacock feathers to make a fly")

		# 现在一个叫Tom的新用户访问了网站

		## 我们使用一个新浏览器会话
		## 确保Jane的信息不会从cookie中泄露出来
		self.browser.refresh()
		self.browser.quit()
		self.browser = webdriver.Firefox()
		self.browser.implicitly_wait(3)

		# Tom访问首页
		# 页面中看不到Jane的清单
		self.browser.get(self.server_url)
		page_text = self.browser.find_element_by_tag_name("body").text
		self.assertNotIn("Buy peacock feathers", page_text)
		self.assertNotIn("make a fly", page_text)

		# Tom输入一个新待办事项，新建一个清单
		# 他不像Jane那样兴趣盎然
		inputbox = self.browser.find_element_by_id("id_new_item")
		inputbox.send_keys("Buy milk")
		inputbox.send_keys(Keys.ENTER)
		self.browser.implicitly_wait(3)

		# Tom获得了他的唯一URL
		tom_list_url = self.browser.current_url
		self.assertRegex(tom_list_url, '/lists/.+')
		self.assertNotEqual(tom_list_url, jane_list_url)

		# 这个页面还是没有Jane的清单
		page_text = self.browser.find_element_by_tag_name("body").text
		self.assertNotIn("Buy peacock feathers", page_text)
		self.assertIn("Buy milk", page_text)